unit SmallStruct.Action.TSSActionFactory;

interface

uses SmallStruct.Core.TSSObjectFactory,
  SmallStruct.Action,
  SmallStruct.Configuration;

type
  TSSActionFactory = class(TSSObjectFactory, ISSActionFactory)
  public
    function GetAction(const AActionName: string): ISSAction;
    procedure FreeAction(AAction: ISSAction);
  protected
    function GetActionConfiguration(const AActionName: string): ISSActionConfiguration;
  end;

implementation

uses
  Dialogs,
  sysutils,
  SmallStruct.Core,
  SmallStruct.Action.TSSAction,
  SmallStruct.BusinessObject,
  SmallStruct.BusinessObject.TSSBusinessObject;

{ TSSActionFactory }

procedure TSSActionFactory.FreeAction(AAction: ISSAction);
var
  BO: ISSBusinessObject;
begin
  while (AAction.GetContext as ISSActionContext).GetBOContainer.GetCount > 0 do
  begin
    BO := (AAction.GetContext as ISSActionContext).GetBOContainer.GetObject(0) as TSSBusinessObject;
    (AAction.GetContext as ISSActionContext).GetBOContainer.RemoveObject(0);
    (((GetSystemObject.GetGlobalObjectTable as ISSObjectContainer).GetObject(SS_BUSINESS_OBJECT_FACTORY_NAME) as ISSBusinessObjectFactory).FreeBusinessObject(BO));
  end;
end;

function TSSActionFactory.GetAction(const AActionName: string): ISSAction;
var
  ActionContext: TSSActionContext;
  Index: Integer;
  BOName: string;
begin
  Result := QueryClass(AActionName).Create(nil) as TSSAction;
  ActionContext := TSSActionContext.Create(nil);
  ActionContext.SetConfiguration(GetActionConfiguration(AActionName));
  Result.SetContext(ActionContext);

  for Index := 0 to (ActionContext.GetConfiguration as ISSActionConfiguration).GetBusinessObjectName.Count - 1 do
  begin
    BOName := (ActionContext.GetConfiguration as ISSActionConfiguration).GetBusinessObjectName.Strings[Index];
    ActionContext.GetBOContainer.AppendObject(BOName,
      (((GetSystemObject.GetGlobalObjectTable as ISSObjectContainer).GetObject(SS_BUSINESS_OBJECT_FACTORY_NAME) as ISSBusinessObjectFactory).GetBusinessObject(BOName)).GetObjectInstance);
  end;
end;

function TSSActionFactory.GetActionConfiguration(const AActionName: string): ISSActionConfiguration;
begin
  Result := (GetSystemObject.GetConfigurationTable as ISSConfigurationTable).QueryActionConfiguration(AActionName) as ISSActionConfiguration;
end;

end.
